import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import './labelmanage.css'
import { AtList, AtListItem,AtSwipeAction } from "taro-ui"
import reactMixin from 'react-mixin';
import myJs from './labelmanageJs.js'
import app from '../../../../main.js'

export default class LabelManage extends Component {

  config = {
    navigationBarTitleText: '患者标签管理'
  }
  constructor () { 
        super(...arguments); 
        this.state = this.getData(); 
    }
  editLabel=(id,name)=>{
    this.navigateTo('../editlabel/editlabel?id='+id+'&name='+name);
  }
  patientChoose=()=>{
    this.navigateTo('../patientchoose/patientchoose');
  }


  init() {
    this.allLabels();
   }
   //获取所有标签的方法
   allLabels() {
    var me = this;
    var data = {};
        app.get("web/patientlabel/many", data, function(res) {
            var data = [];
            for(var i = 0; i < res.data.result.length; i++) {
                var m = {
                    id: res.data.result[i].id,
                    name: res.data.result[i].name,
                    is_selected: res.data.result[i].is_selected,
                    amount: res.data.result[i].amount,
                }
                data.push(m);
            }
            me.setState({labels: {items: data}});
        });
    }
  //删除标签
  deleteLabels(label_id,label_name) {
    var me = this;
    var data = {label_id: label_id};
    Taro.showModal({
      title:'提示',
      content:'确定删除‘'+label_name+'’标签',
    }).then(res=>{
      if(res.confirm){
        app.post("web/patientlabel/delete", data, function(res) {
          console.log(res);
          me.redirectTo('../labelmanage/labelmanage')
        });
      }
    })
        
    }
 
  render () {
    
    const { lables } = this.state;

    return (
      <View className='lable-manage'>
        <AtList className='add-tag'>
          <AtListItem title='添加新标签' arrow='right' onClick={this.navigateTo.bind(this,'../editlabel/editlabel?id=0&name=')} thumb={app.imageUri+'img/tianjiabingli.png'}/>
          <AtListItem title='我的患者可选标签' arrow='right' onClick={this.patientChoose} thumb={app.imageUri+'img/touxiang.png'}/>
        </AtList>
        <AtList className='tags'>
          <AtListItem title='全部患者标签' disabled  className='gray-list'/>
          {lables.items.map((item,index) =>
              <AtSwipeAction
              onClick={this.deleteLabels.bind(this,item.id,item.name)}
              options={[
              {
                text: '删除',
                style: {
                  backgroundColor: '#FF4949'
                }
              }
            ]} key={index}>
                  <AtListItem 
                  title={item.name} 
                  arrow='right' 
                  onClick={this.editLabel.bind(this,item.id,item.name)} 
                  extraText={String(item.amount)} 
                />
              </AtSwipeAction>
            
          )}
        </AtList>
      </View>
    )
  }
}
reactMixin.onClass(LabelManage,myJs);
